package com.th.admin.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.th.common.core.controller.BaseController;
import com.th.common.core.domain.AjaxPageResult;
import com.th.common.core.domain.AjaxResult;
import com.th.system.domain.UsQuaVerify;
import com.th.system.domain.vo.QuaVer.*;
import com.th.system.service.ISysQuaVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = {"资质认证"})
@RestController
@RequestMapping("/system/qua")
@Slf4j
public class UsQuaVerifyController extends BaseController {

    private static final int TYPE_DESIGN = 1;
    private static final int TYPE_WORKROOM = 2;
    private static final int TYPE_COMPANY = 3;



    @Autowired
    private ISysQuaVerifyService quaVerifyService;


    /**
     * 分页查询待审核设计师资质认证数据记录
     */
    @GetMapping(value = "/designer")
    @ApiOperation("分页查询待审核设计师资质认证数据记录")
    public AjaxPageResult pageDesigner(@RequestParam("status") int status,@RequestParam("page") int page, @RequestParam("pagesize") int pageSize) {
        List<UsQuaVerify> list = quaVerifyService.getWaitQuaVerByType(TYPE_DESIGN, status, page, pageSize);

        List<QuaDesignerVo> desList = new ArrayList<>();
        for (UsQuaVerify el : list) {
            JSONObject jsonObject = JSONObject.parseObject(el.getExamineJson());

            QuaDesignerVo designerVo = new QuaDesignerVo();
            BeanUtils.copyProperties(el, designerVo);
            designerVo.setExamine_json(JSON.toJavaObject(jsonObject, ExamineDataDesignerVo.class));

            desList.add(designerVo);
        }
        System.out.println();
        System.out.println(desList.toString());

        return AjaxPageResult.success("", page, desList.size(), desList);
    }


    /**
     * 分页查询待审核工作室资质认证数据记录
     */
    @GetMapping(value = "/workroom")
    @ApiOperation("分页查询待审核工作室资质认证数据记录")
    public AjaxPageResult pageWorkroom(@RequestParam("status") int status,@RequestParam("page") int page, @RequestParam("pagesize") int pageSize) {
        List<UsQuaVerify> list = quaVerifyService.getWaitQuaVerByType(TYPE_WORKROOM, status, page, pageSize);

        List<QuaWorkroomVo> wkList = new ArrayList<>();
        for (UsQuaVerify el : list) {
            JSONObject jsonObject = JSONObject.parseObject(el.getExamineJson());

            QuaWorkroomVo workroomVo = new QuaWorkroomVo();
            BeanUtils.copyProperties(el, workroomVo);
            workroomVo.setExamine_json(JSON.toJavaObject(jsonObject, ExamineDataWorkroomVo.class));

            wkList.add(workroomVo);
        }
        System.out.println();
        System.out.println(wkList.toString());

        return AjaxPageResult.success("", page, wkList.size(), wkList);
    }

    /**
     * 分页查询待审核公司资质认证数据记录
     */
    @GetMapping(value = "/company")
    @ApiOperation("分页查询待审核公司资质认证数据记录")
    public AjaxPageResult pageCompany(@RequestParam("status") int status,@RequestParam("page") int page, @RequestParam("pagesize") int pageSize) {
        List<UsQuaVerify> list = quaVerifyService.getWaitQuaVerByType(TYPE_COMPANY, status, page, pageSize);

        List<QuaCompanyVo> comList = new ArrayList<>();
        for (UsQuaVerify el : list) {
            JSONObject jsonObject = JSONObject.parseObject(el.getExamineJson());

            QuaCompanyVo quaCompanyVo = new QuaCompanyVo();
            BeanUtils.copyProperties(el, quaCompanyVo);
            quaCompanyVo.setExamine_json(JSON.toJavaObject(jsonObject, ExamineDateCompanyVo.class));

            comList.add(quaCompanyVo);
        }
        System.out.println();
        System.out.println(comList.toString());

        return AjaxPageResult.success("", page, comList.size(), comList);
    }

    @PostMapping(value = "/examine/designer")
    @ApiOperation("修改审核结果")
    public AjaxResult modifyDesignerStauts(Integer id, Integer examineResultStatus, String examineResultMessage) {

        boolean flag = quaVerifyService.updateStatusById(id, examineResultStatus, examineResultMessage);
        if (!flag) {
            return AjaxResult.error();
        }

        return AjaxResult.success();
    }

    @GetMapping(value = "/designer/{examineResultStatus}")
    @ApiOperation("根据审核状态查询")
    public AjaxPageResult pageDesignerByAuthStauts(@PathVariable("examineResultStatus") int stauts, @RequestParam("page") int page, @RequestParam("pagesize") int pageSize) {

        List<UsQuaVerify> list = quaVerifyService.getWaitQuaVerByType(TYPE_DESIGN, stauts, page, pageSize);

        List<QuaALLDesignerVO> desList = new ArrayList<>();
        for (UsQuaVerify el : list) {
            JSONObject jsonObject = JSONObject.parseObject(el.getExamineJson());

            QuaALLDesignerVO designerVo = new QuaALLDesignerVO();
            BeanUtils.copyProperties(el, designerVo);
            designerVo.setExamine_json(JSON.toJavaObject(jsonObject, ExamineDataDesignerVo.class));

            desList.add(designerVo);
        }
        System.out.println();
        System.out.println(desList.toString());

        return AjaxPageResult.success("", page, desList.size(), desList);

    }

    /**
     * 资质管理员无法调用该接口，系统的管理员admin才可以调用
     */
    @GetMapping(value = "/delete/{id}")
    @ApiOperation("删除审核记录")
    public AjaxResult deleteQuaVer(@PathVariable("id") int id) {

        boolean flag = quaVerifyService.idIsExsit(id);
        if(!flag){
            return AjaxResult.error("id 不存在");
        }

        flag = quaVerifyService.deleteQuaVerById(id);
        if (!flag) {
            return AjaxResult.error();
        }

        return AjaxResult.success("删除成功");
    }
}